﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmApiLibrary.datatypes;
using SmApiLibrary.helps;

namespace SmApiLibrary.libs
{
    class WeiBoSohu : IWeiBo
    {
        #region 发微博
        public string AddWeiBo(string content)
        {
            string url = "http://api.t.sohu.com/statuses/update.json";
            string postData = "status=" + Utility.UrlEncode(content);// +"&clientip=127.0.0.1" + "&jing=&wei=";
            string response = OAuth.OAuthWebRequest(Method.POST, url, postData);
            return response;
        }

        public string AddWeiBoPic(string content, string picfilename)
        {
            // http://api.t.sohu.com/statuses/upload.xml
            string url = "http://api.t.sohu.com/statuses/upload.json";
            string postData = "status=" + Utility.UrlEncode(content);// +"&pic=" + picfilename;
            string response = OAuth.OAuthWebRequest(Method.POST, url, postData, picfilename);
            return response;
        }

        #endregion

        public SohuUser GetWeiBoUserInfo()
        {
            string url = "http://api.t.sohu.com/users/show.json";
            string result = OAuth.OAuthWebRequest(Method.GET, url, null, null);

            return GetSohuUser(result);
        }

        private SohuUser GetSohuUser(string result)
        {
            SohuUser suer = JsonHelper.JsonDeserialize<SohuUser>(result);
            return suer;
        }

        public string GetUserId()
        {
            string _userId = GetWeiBoUserInfo().Id;
            return _userId;
        }


        public OAuthSohu OAuth { get; set; }

        public bool IsEnabled
        {
            get
            {
                return !string.IsNullOrEmpty(OAuth.Token);
            }
        }

        protected Random RandomGenerator = new Random();
        public WeiBoSohu()
        {
            if (OAuth == null)
            {
                OAuth = new OAuthSohu();
            }
        }

        public void SetOAuth(string token, string secret)
        {
            OAuth.Token = token;
            OAuth.TokenSecret = secret;
        }

        #region IWeiBo Members


        public WeiBoType UserType
        {
            get { return WeiBoType.Sohu; }
        }

        public OAuthBase GetOAuth()
        {
            return OAuth;
        }

        #endregion
            
    }
}
